//
//  LYDPopoverSecondView.m
//  HAODE
//
//  Created by LiYinDi on 16/5/26.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "FilterSecondView.h"
#import "FilterSecondTableViewCell.h"
#import "UIViewExt.h"
#import "FilterSecondLevelModel.h"
#import "FilterFirstLevelModel.h"
#import "MJRefresh.h"
#import "TSFooterRefresh.h"
#define kUIScreenHeight [UIScreen mainScreen].bounds.size.height
#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
@interface FilterSecondView() <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, FilterSecondCellDelegate>
{
//    MJRefreshFooterView* _footer;
    int pageNumber;
    NSString *pageSize;
    NSMutableArray *_listArray;
    NSMutableArray *firstLevelModelArray;
//    NSMutableArray *selectArr;
    UIButton *startTimeButton;
    UIButton *endTimeButton;
    NSString *StartOrEnd;
    UITextField*searchTextField;
    FilterSecondLevelModel *selectedSecondModel;
    UIButton *_confirmBtn;
    NSString *isSearch;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *theContentView;
@property (nonatomic, strong) UIButton *alphaBtn;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *topLineLabel;
@property (nonatomic, strong) UIView *dateView;
@property (nonatomic, strong) UIButton *dateConfirmBtn;
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) NSString *titleStr;
@end
@implementation FilterSecondView

-(instancetype)init:(NSMutableArray *)listArray  secondLevelModel:(FilterSecondLevelModel *)secondLevelModel title:(NSString *)title isLikeSearch:(NSString*)isLikeSearch totalCount:(NSString*)totalCount typeCode:(NSString*)typeCode  itemsArry:(NSMutableArray*)itemsArry andIsSingle:(NSString *)isSingle
{
    self=[super init];
    if (self) {
        pageSize = @"20";
        _listArray=listArray;
        _totalCount=[NSString stringWithFormat:@"%@",  totalCount];
        _typeCode=typeCode;
        _items=[itemsArry copy];
        
        // 这里为了处理多选
//        selectArr = [NSMutableArray array];
        
        if (self.didSelectArr.count == 0) {
            self.didSelectArr =[NSMutableArray array];
        }
        
        self.titleStr = title;
        
        isSearch = @"N";
        
        selectedSecondModel=secondLevelModel;
        [self setup:title:isLikeSearch :isSingle];
    }
    return self;
}

- (void)setup:(NSString *)titleStr :(NSString *)isLikeSearch :(NSString *)isSingle {
   
    _alphaBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_alphaBtn addTarget:self action:@selector(alphaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_alphaBtn setBackgroundColor: [UIColor clearColor]];
    [self addSubview:_alphaBtn];
    
    _theContentView=[[UIView alloc] init];
    _theContentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_theContentView];
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelBtn setTitleColor:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_theContentView addSubview:_cancelBtn];
    
    _titleLabel=[[UILabel alloc] init];
    _titleLabel.text=titleStr;
    _titleLabel.textColor=[UIColor blackColor];
    _titleLabel.textAlignment=NSTextAlignmentCenter;
    [_titleLabel setFont:[UIFont systemFontOfSize:17]];
    [_theContentView addSubview:_titleLabel];
    
    _topLineLabel=[[UILabel alloc] init];
    [_topLineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [_theContentView addSubview:_topLineLabel];
    
    _tableView = [[UITableView alloc] init];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_theContentView addSubview:_tableView];
    
    // 这里为了处理多选
    if ([isSingle isEqualToString:@"N"]) {
        _confirmBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundColor:lanse];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_theContentView addSubview:_confirmBtn];
    }

    _dateView=[[UIView alloc] init];
    [self addSubview:_dateView];
    
    _dateConfirmBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_dateConfirmBtn setBackgroundColor:[UIColor colorWithRed:8.0f/255.0f green:128.0f/255.0f blue:198.0f/255.0f alpha:1.0f]];
    [_dateConfirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_dateConfirmBtn setTintColor:[UIColor whiteColor]];
    [_dateConfirmBtn addTarget:self action:@selector(dateConfirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_dateView addSubview:_dateConfirmBtn];
    
    _datePicker = [[UIDatePicker alloc] init];
    [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.backgroundColor=[UIColor whiteColor];
    [_dateView addSubview:_datePicker];
    _dateView.hidden=YES;
    
    
    if ([isLikeSearch isEqualToString:@"Y"]) {
        UIView*Hview=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        Hview.backgroundColor=zuiqianhuiColor;
        _tableView.tableHeaderView=Hview;
        pageNumber=1;
        UIButton *sxBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(7, 12, 25, 25)];
        [sxBtn1 setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
        [Hview addSubview:sxBtn1];
        
        
        searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(sxBtn1.right,0, ScreenWidth-90,50)];
        [searchTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
        
        NSString *strFormart = [NSString stringWithFormat:@" 请搜索%@", self.titleStr];
        searchTextField.placeholder = strFormart;//@" 请输入会员名称/昵称/手机号"; //默认显示的字
        searchTextField.font = [UIFont systemFontOfSize: 14];
        searchTextField.tag=22;
        //    searchTextField.keyboardType = UIKeyboardTypeDecimalPad;
        searchTextField.returnKeyType = UIReturnKeyDone;
        searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        searchTextField.delegate = self;
        [Hview addSubview:searchTextField];
        
        UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
        myToolBar.backgroundColor=jianpancolor;
        UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
        [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:tubtnDone];
        UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
        [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
        [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
        btnDone111 .titleLabel .font=[UIFont systemFontOfSize:16];
        [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:btnDone111];
        myToolBar.alpha=alp;
        UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
        [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
        [myToolBar addSubview:dabtn];
        searchTextField.inputAccessoryView = myToolBar;
       
        [self setUpRefresh];
       
    }else{
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
    }
    if ([_typeCode isEqualToString:@"i.ItemTypeCode"]) {

        [self requestFilterList];
    }

}

-(void)requestFilterList {
    
    _listArray=[[NSMutableArray alloc]init];
     firstLevelModelArray=[[NSMutableArray alloc]init];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
   if ([_typeCode isEqualToString:@"i.ItemTypeCode"]) {
        [param setValue:@"inventory" forKey:@"filterType"];
        [param setValue:_items forKey:@"searchBeans"];
    }else{
        [param setValue:self.typeStr forKey:@"filterType"];
        NSMutableArray *searchBeans=[NSMutableArray array];
        NSMutableDictionary *itemsDic= [NSMutableDictionary dictionary];
        [itemsDic setObject:_typeCode forKey:@"typeCode"];
        [itemsDic setObject:_titleLabel.text forKey:@"typeName"];
        [itemsDic setObject:@"String" forKey:@"dataType"];
        [itemsDic setObject:@"Y" forKey:@"isSingle"];
        [itemsDic setObject:@"Y" forKey:@"isLikeSearch"];
        [itemsDic setObject:@"1" forKey:@"pageIndex"];
        [itemsDic setObject:pageSize forKey:@"pageSize"];
        [itemsDic setObject:searchTextField.text forKey:@"sword"];
        [searchBeans addObject:itemsDic];
        [param setValue:searchBeans forKey:@"searchBeans"];
    }
   
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self];
    NSLog(@"arrr#######====%@",param);
    [HttpRequest requestData:@"report/advancedFilterList" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                NSLog(@"arrr#######====%@",severDataDict.dataDict);
                [MBProgressHUD hideProgressHUDInternal:self];
                NSMutableArray* arr=[[NSMutableArray alloc]init];
                arr=[severDataDict.dataDict objectForKey:@"DataLine"];
                for (NSDictionary* dicc in arr ) {
                    FilterFirstLevelModel *firstLevelModel=[FilterFirstLevelModel dataModelWithDict:dicc];
                    [firstLevelModelArray addObject:firstLevelModel];
                }
                FilterFirstLevelModel *firstModel = firstLevelModelArray[self.parentIndex.intValue];
                
                for (int i = 0; i<[firstModel.secondModelArray count]; i++) {
                    if ([_listArray containsObject:[firstModel.secondModelArray objectAtIndex:i]] == NO) {
                        [_listArray addObject:[firstModel.secondModelArray objectAtIndex:i]];
                    }
                }
//                维记
                
                _totalCount=[NSString stringWithFormat:@"%@", firstModel.totalCount];
//                _listArray=[firstModel.secondModelArray mutableCopy]; 睿本
                [_tableView reloadData];
                
            } else {
                [MBProgressHUD hideProgressHUDInternal:self];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self];
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self];
        }
    }];
    
}

// 刷新
- (void)setUpRefresh {
    
    _tableView.mj_footer = [TSFooterRefresh footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProductList)];
    
}

// 上拉刷新
- (void)loadMoreProductList{
    
    if (_totalCount.floatValue/20.0 > pageNumber) {
        pageNumber++;
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *param = [NSMutableDictionary new];
        [param setValue:Token forKey:@"tokenId"];
        if ([_typeCode isEqualToString:@"i.ItemTypeCode"]) {
            [param setValue:@"inventory" forKey:@"filterType"];
            [param setValue:_items forKey:@"searchBeans"];
        }else{
            
            NSMutableArray *searchBeans=[NSMutableArray array];
            NSMutableDictionary *itemsDic= [NSMutableDictionary dictionary];
            [param setValue:self.typeStr forKey:@"filterType"];
            [itemsDic setObject:_typeCode forKey:@"typeCode"];
            [itemsDic setObject:_titleLabel.text forKey:@"typeName"];
            [itemsDic setObject:@"String" forKey:@"dataType"];
            [itemsDic setObject:@"Y" forKey:@"isSingle"];
            [itemsDic setObject:@"Y" forKey:@"isLikeSearch"];
            [itemsDic setObject:searchTextField.text forKey:@"sword"];
            [itemsDic setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
            [itemsDic setObject:pageSize forKey:@"pageSize"];
            [searchBeans addObject:itemsDic];
            [param setValue:searchBeans forKey:@"searchBeans"];
        }
        [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self];
        [HttpRequest requestData:@"report/advancedFilterList" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
            NSLog(@"%@", severDataDict.dataDict);
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [MBProgressHUD hideProgressHUDInternal:self];
                    firstLevelModelArray=[[NSMutableArray alloc]init];
                    
                    NSMutableArray* arr=[[NSMutableArray alloc]init];
                    arr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arr )
                    {
                        FilterFirstLevelModel *firstLevelModel=[FilterFirstLevelModel dataModelWithDict:dicc];
                        [firstLevelModelArray addObject:firstLevelModel];
                    }
                    
                    FilterFirstLevelModel *firstModel = firstLevelModelArray[self.parentIndex.intValue];
//                     [_listArray arrayByAddingObjectsFromArray:firstModel.secondModelArray];
//                        NSLog(@"2===========%@",firstModel.secondModelArray);
                    
                    
                    
                    for (int i = 0; i<[firstModel.secondModelArray count]; i++) {
                        if ([_listArray containsObject:[firstModel.secondModelArray objectAtIndex:i]] == NO) {
                            [_listArray addObject:[firstModel.secondModelArray objectAtIndex:i]];
                        }
                    }
                    
                    if (_totalCount.intValue < pageSize.intValue) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    } else if(_totalCount.intValue > pageSize.intValue){
                        [_tableView.mj_footer resetNoMoreData];
                    }
                    
                    
                    [_tableView reloadData];
                }else {
                    [MBProgressHUD hideProgressHUDInternal:self];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [MBProgressHUD showTextHudSub:requestResultStr view:self];
                }
                
            } else {
                [MBProgressHUD hideProgressHUDInternal:self];
                [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self];
            }
            
        }];
    }else{
        NSString *requestResultStr=@"没有更多数据了！";
        [MBProgressHUD showTextHudSub:requestResultStr view:self];
        [_tableView.mj_footer endRefreshingWithNoMoreData];
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    isSearch = @"Y";
    [self requestFilterList];
    [searchTextField resignFirstResponder];
    return YES;
}

-(void)OnTapDone{
    isSearch = @"Y";
    [self requestFilterList];
    [searchTextField resignFirstResponder];
}

-(void)alphaBtnClick {
    
    [self dismiss];
}
-(void)cancelBtnClick {
    
    [self dismiss];
}
-(void)dateConfirmBtnClick {
   
    _dateView.hidden=YES;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    
    NSString *str=[NSString stringWithFormat:@"%@", [df stringFromDate:_datePicker.date]];
    
    if ([StartOrEnd isEqualToString:@"Start"]) {
        
        [startTimeButton setTitle:str forState:UIControlStateNormal];
        [startTimeButton setTitleColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        
    } else {
        [endTimeButton setTitle:str forState:UIControlStateNormal];
        [endTimeButton setTitleColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    }

    
}
- (void)dateChanged:(id)sender {
    
    UIDatePicker* picker = (UIDatePicker*)sender;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString *str=[NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
    if ([StartOrEnd isEqualToString:@"Start"]) {
        [startTimeButton setTitle:str forState:UIControlStateNormal];
    }else{
        [endTimeButton setTitle:str forState:UIControlStateNormal];
    }
}
-(void)startTimeButtonClick:(nonnull UIButton *)btn
{
    startTimeButton=btn;
    StartOrEnd=@"Start";
    _dateView.hidden=NO;
}
-(void)endTimeButtonClick:(nonnull UIButton *)btn
{
    endTimeButton=btn;
    StartOrEnd=@"End";
    _dateView.hidden=NO;
}

- (void)dismiss {
    
    [self removeFromSuperview];
    
}

#pragma mark - tableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const  CellIdentifier = @"FilterTableViewCellIdentifier";
    FilterSecondTableViewCell *cell=( FilterSecondTableViewCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FilterSecondTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FilterSecondLevelModel *model=_listArray[indexPath.row];
    cell.delegate=self;
    cell.timeConfirmBtn.tag=indexPath.row;
    cell.model=model;
//    cell.startTimeTextField.delegate=self;
//    cell.endTimeTextField.delegate=self;
//    cell.startTimeTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
//    cell.endTimeTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
//    [cell setUserInteractionEnabled:YES];
    
    cell.selectedImageView.hidden=YES;

    
//    if ([self.didSelectArr containsObject:[_listArray objectAtIndex:indexPath.row]]) {
//        cell.selectedImageView.hidden=NO;
//    }
    
    
    // 这里为了处理多选
    NSLog(@"self.didSelectArr == %@", self.didSelectArr);
    if (self.didSelectArr.count >0) {
        for (FilterSecondLevelModel *model1 in self.didSelectArr) {

            if ([model1.itemName isEqualToString:model.itemName]) {
                cell.selectedImageView.hidden=NO;
            }

        }
    } else{
        
        
        if ([selectedSecondModel.isCustom isEqualToString:@"Y"]) {
            [cell.startTimeButton setTitle:selectedSecondModel.minValue forState:UIControlStateNormal];
            [cell.endTimeButton setTitle:selectedSecondModel.maxValue forState:UIControlStateNormal];
            startTimeButton=cell.startTimeButton;
            endTimeButton=cell.endTimeButton;
            [startTimeButton setTitleColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] forState:UIControlStateNormal];
            [endTimeButton setTitleColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        } else {
            [cell.startTimeButton setTitle:@"开始时间" forState:UIControlStateNormal];
            [cell.endTimeButton setTitle:@"结束时间" forState:UIControlStateNormal];
        }
        
        cell.selectedImageView.hidden=YES;
        
        if ([model.itemName isEqualToString:selectedSecondModel.itemName]) {
            cell.selectedImageView.hidden=NO;
            [self.didSelectArr addObject:model];
        }
    }
    
    return cell;
    
}


#pragma mark FilterSecondTableViewCellDelegate
-(void)timeConfirmButtonClick:(NSUInteger)tag
{
    if (startTimeButton.titleLabel.text.length==0||[startTimeButton.titleLabel.text isEqualToString:@"开始时间"]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择开始时间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
        
    } else if (endTimeButton.titleLabel.text.length==0||[endTimeButton.titleLabel.text isEqualToString:@"结束时间"]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择结束时间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    } else {
        FilterSecondLevelModel *model=_listArray[tag];
        model.minValue=startTimeButton.titleLabel.text;
        model.maxValue=endTimeButton.titleLabel.text;
        model.itemName=[NSString stringWithFormat:@"%@~%@",startTimeButton.titleLabel.text,endTimeButton.titleLabel.text];
        
        //    NSLog(@"~~~~~%@%@",startTimeTextField.text,endTimeTextField.text);
        if ([self.delegate respondsToSelector:@selector(FilterSecondView:cellIndex:)]) {
            [self.delegate FilterSecondView: model cellIndex:tag];
        }
        [self dismiss];

    }
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterSecondLevelModel *model=_listArray[indexPath.row];
    NSLog(@"----%@",model.itemName);
    if ([model.itemName isEqualToString:@"自定义"]) {
        
    }else{
        // 这里为了处理多选
        if ([self.isSingle isEqualToString:@"N"]) {
            
            FilterSecondTableViewCell *cell=(FilterSecondTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            
            
            if ([model.itemName isEqualToString:@"全部"]) {
                [self.didSelectArr removeAllObjects];
                
                
                for (int i= 0; i<_listArray.count; i++) {
                    FilterSecondLevelModel *model1 = _listArray[i];
                    if ([model1.itemName isEqualToString:@"全部"]) {
                        [self.didSelectArr removeObject:model1];
                        NSIndexPath *idex = [NSIndexPath indexPathForRow:i inSection:0];
                        FilterSecondTableViewCell *cell=(FilterSecondTableViewCell *)[self.tableView cellForRowAtIndexPath:idex];
                        cell.selectedImageView.hidden=NO;
                        [self confirmBtnClick];
                    } else{
                        NSIndexPath *idex = [NSIndexPath indexPathForRow:i inSection:0];
                        FilterSecondTableViewCell *cell=(FilterSecondTableViewCell *)[self.tableView cellForRowAtIndexPath:idex];
                        cell.selectedImageView.hidden=YES;
                    }
                }
                
            } else{
                
                NSLog(@"----%ld",indexPath.row);
                for (int i= 0; i<_listArray.count; i++) {
                    FilterSecondLevelModel *model1 = _listArray[i];
                    if ([model1.itemValue isEqualToString:@"ALL"]) {
                        [self.didSelectArr removeObject:model1];
                        NSIndexPath *idex = [NSIndexPath indexPathForRow:i inSection:0];
                        FilterSecondTableViewCell *cell=(FilterSecondTableViewCell *)[self.tableView cellForRowAtIndexPath:idex];
                        cell.selectedImageView.hidden=YES;
                        cell.selected = NO;
                    }
                }
                
                NSIndexPath *idex = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
                FilterSecondTableViewCell *cell=(FilterSecondTableViewCell *)[self.tableView cellForRowAtIndexPath:idex];
                
                if (![_didSelectArr containsObject:_listArray[indexPath.row]]) {
                    cell.selectedImageView.hidden=NO;
                    [_didSelectArr addObject:_listArray[indexPath.row]];
                } else{
                    cell.selectedImageView.hidden=YES;
                    [_didSelectArr removeObject:_listArray[indexPath.row]];
                }
                
//                if (cell.selected == YES ) {
//
//                    FilterSecondLevelModel *model1 = _listArray[indexPath.row];
//                    if (![self.didSelectArr containsObject:model1]) {
//                        cell.selectedImageView.hidden=NO;
//                        [self.didSelectArr addObject:_listArray[indexPath.row]];
//                    } else{
//                        cell.selectedImageView.hidden=YES;
//                        [self.didSelectArr removeObject:_listArray[indexPath.row]];
//                    }
//
//
//                } else{
//
//                    cell.selectedImageView.hidden=YES;
//                    [self.didSelectArr removeObject:_listArray[indexPath.row]];
//                }
                
               
                if ([isSearch isEqualToString:@"Y"]) {
                    if (self.didSelectArr.count > 1) {
                        FilterSecondLevelModel *model = self.didSelectArr[0];
                        if ([model.itemName isEqualToString:@"全部"]) {
                            [self.didSelectArr removeObjectAtIndex:0];
                        }
                    }
                    
                }
                
            }
            
            
        } else{
            if ([self.delegate respondsToSelector:@selector(FilterSecondView:cellIndex:)]) {
                [self.delegate FilterSecondView:model cellIndex:indexPath.row];
            }
            [self dismiss];
        }
    }
}


#pragma mark -
#pragma mark 点击确定 这里为了处理多选
-(void)confirmBtnClick {
    
//
//    if (selectArr.count == 0 && self.didSelectArr.count > 0) {
//        for (FilterSecondLevelModel *model in self.didSelectArr) {
//            [selectArr addObject:model];
//        }
//    }
//
    NSLog(@"didSelectArr == %@", self.didSelectArr);
    if (self.didSelectArr.count == 0) {
        for (int i =0 ; i < _listArray.count; i++) {
            FilterSecondLevelModel *model=_listArray[i];
            if ([model.itemName isEqualToString:@"全部"]) {
                [self.didSelectArr addObject:model];
                NSLog(@"didSelectArr == %@", self.didSelectArr);
                if ([self.delegate respondsToSelector:@selector(FilterSecondView:cellIndex:)]) {
                    [self.delegate FilterSecondView:model cellIndex:0];
                }
                [self dismiss];
            }
            
        }
    } else{
        if ([self.delegate respondsToSelector:@selector(FilterSecondViewWithArr:)]) {
            [self.delegate FilterSecondViewWithArr:self.didSelectArr];
        }
        [self dismiss];
    }
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    [_theContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_alphaBtn.mas_right);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.right.equalTo(self.mas_right);
    }];
    
    
    
    
    if (is_iPhoneX) {
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left).with.offset(10);
            make.top.equalTo(_theContentView.mas_top).with.offset(25+24);
            make.height.equalTo(@30);
            make.width.equalTo(@40);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_theContentView.mas_centerX);
            make.top.equalTo(_theContentView.mas_top).with.offset(25+24);
            make.height.equalTo(@30);
            make.width.equalTo(@100);
            
        }];
        
        [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left).with.offset(0);
            make.top.equalTo(_theContentView.mas_top).with.offset(63+24);
            make.height.equalTo(@0.5);
            make.right.equalTo(_theContentView.mas_right).with.offset(0);
        }];
    } else{
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left).with.offset(10);
            make.top.equalTo(_theContentView.mas_top).with.offset(25);
            make.height.equalTo(@30);
            make.width.equalTo(@40);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_theContentView.mas_centerX);
            make.top.equalTo(_theContentView.mas_top).with.offset(25);
            make.height.equalTo(@30);
            make.width.equalTo(@100);
            
        }];
        
        [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left).with.offset(0);
            make.top.equalTo(_theContentView.mas_top).with.offset(63);
            make.height.equalTo(@0.5);
            make.right.equalTo(_theContentView.mas_right).with.offset(0);
        }];
    }
    
    
    
    // 这里为了处理多选
    if ([self.isSingle isEqualToString:@"N"]) {
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left);
            make.top.equalTo(_topLineLabel.mas_bottom);
            make.bottom.equalTo(_theContentView.mas_bottom).with.offset(-(45+TabbarSafeBottomMargin));
            make.right.equalTo(_theContentView.mas_right);
        }];
        
        [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left);
            make.bottom.equalTo(_theContentView.mas_bottom).offset(-TabbarSafeBottomMargin);
            make.height.equalTo(@45);
            make.right.equalTo(_theContentView.mas_right);
        }];
    } else{
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_theContentView.mas_left);
            make.top.equalTo(_topLineLabel.mas_bottom);
            make.bottom.equalTo(_theContentView.mas_bottom).with.offset(-TabbarSafeBottomMargin);
            make.right.equalTo(_theContentView.mas_right);
        }];
    }

    [_dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_bottom).with.offset(-195);
        make.height.equalTo(@195);
        make.right.equalTo(self.mas_right);
    }];
    [_dateConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_dateView.mas_left);
        make.right.equalTo(_dateView.mas_right);
        make.bottom.equalTo(_dateView.mas_bottom);
        make.height.equalTo(@44);
    }];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_dateView.mas_left);
        make.right.equalTo(_dateView.mas_right);
        make.bottom.equalTo(_dateConfirmBtn.mas_top);
        make.height.equalTo(@150);
    }];

}

@end
